package com.enhe.gck.fg;

import com.alibaba.fastjson.JSON;
import com.enhe.gck.com.oracle.OracleController;
import com.enhe.gck.com.oracle.OracleEntity;
import com.enhe.gck.communal.AsyncImpl;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.pojo.Customer;
import com.enhe.gck.pojo.PickupPoint;
import com.kuangkie.carbon.fg.*;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.FGRecord;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import com.kuangkie.carbon.record.FGRecordComplexus;
import com.kuangkie.carbon.record.ProRecord;
import com.enhe.gck.CarbonBaseConstant; //模型Code
import com.enhe.gck.CarbonRelationType; //关系类型Code
import com.enhe.gck.item.PickupPointHWANE17409Item; // 模型属性Code

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository(value = "hwane17409366042330866425862")
public class PickupPointHWANE17409BNB implements SecondRoundImproveFuncGroup ,FuseCallBackFuncGroup{

	@Resource
	private AsyncImpl asyncImpl;
	@Override
	public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
			ImproveResultFactory improveResultFactory) {
		PickupPoint pickupPoint=proRecord.getBean(PickupPoint.class);
		if(proRecord.isNew()){
			improveResultFactory.getCurrentProRecordOpsBuilder()
					.putAttribute(PickupPointHWANE17409Item.基本属性组_是否删除,false)
					.putAttribute(PickupPointHWANE17409Item.基本属性组_是否同步,false);
		}
		improveResultFactory.getCurrentProRecordOpsBuilder()
				.putAttribute(PickupPointHWANE17409Item.基本属性组_不含税运费每吨, PublicMethod.divideBigDecimal(pickupPoint.getTaxFare(),PublicMethod.addBigDecimal(1,PublicMethod.divideBigDecimal(pickupPoint.getTaxCode(),100)),2,null));
	}

	@Override
	public boolean afterFusition(FuncGroupContext funcGroupContext,FuseContext fuseContext, String s) {
		if(fuseContext.isNewCreate()){
			PickupPoint pickupPoint=CarbonPanel.getRecordDiscover(CarbonBaseConstant.提货点_thd).getRecordBean(s,PickupPoint.class,2);
			if(!pickupPoint.getFlagDelete()&&!pickupPoint.getFlagSync()){
				//触发数据同步
				List<OracleEntity.Pickup> list=new ArrayList<>();
				OracleEntity.Pickup info=new OracleEntity.Pickup();
				info.setSubareaName(pickupPoint.getName());
				info.setContact(pickupPoint.getContact());
				info.setPhone(pickupPoint.getPhone());
				info.setZip("");
				info.setFare(String.valueOf(pickupPoint.getFare()));
				info.setTaxCode(pickupPoint.getTaxCode()+"%");
				info.setTaxFare(String.valueOf(pickupPoint.getTaxFare()));
				info.setEhId(pickupPoint.getRecordCode());
				info.setAddress(pickupPoint.getAddress());
//				if(pickupPoint.getCustomer()!=null){
//					info.setCustomerId(pickupPoint.getCustomer().getCustomerId());
//					info.setCustomerCode(pickupPoint.getCustomer().getCustomerName());
//				}
				info.setCustomerId("");
				info.setCustomerCode("");
				list.add(info);
				asyncImpl.savePickup(list);
			}
		}
		return true;
	}
}
